package service_edu.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.zeta.eduservice.entity.vo.front.PdfQuestionVo;
import com.zeta.eduservice.utils.Download;

public class test1 {
	  public static final String RESULT = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\hello.pdf";
	  
	    /**
	     *   创建一个PDF文件：hello.pdf 
	     * @param    args    no arguments needed
	     */
	  @Test
	  public void test1() {
		
		  String pdfDownloadPath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\questionFolderDownload";

		  File file = new File(pdfDownloadPath+"\\"+"22233");
	
			if(!file.exists()){//如果文件夹不存在
				file.mkdir();//创建文件夹
			}
	  }
	@Test
	public void test2() throws DocumentException, IOException {
		  String pdfDownloadPath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\questionFolderDownload\\file.pdf";

		 Document document = new Document(PageSize.A4);
	        // 第二步 获取PdfWriter实例
	        PdfWriter.getInstance(document, new FileOutputStream(pdfDownloadPath));
	        // 第三步 打开文档
	        document.open();
	      
	        BaseFont baseFont = BaseFont.createFont("STSong-Light",
	        		"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	        Font font = new Font(baseFont, 10, Font.BOLD);
	        Paragraph p1 = new Paragraph();
	        PdfPTable table = new PdfPTable(1+3);
	        table.setWidthPercentage(90);
	        table.setSpacingBefore(10f);
			table.setSpacingAfter(10f);
			
			table.addCell(new Phrase("题号",font));
			String[] s= {"一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四"};
			for(int i=0;i<3-1;i++) {
				table.addCell(new Paragraph(s[i],font)); 
			}
			table.addCell(new Paragraph("总分",font));
			table.addCell(new Paragraph("复核人",font));
			
			table.addCell(new Paragraph("得分",font));
			for(int i=0;i<=3-1;i++) {
				table.addCell("");
			}
			table.addCell(new Paragraph("阅卷人",font));
			
			p1.add(table);
	        
	        document.close();

	}
	@Test
	public void test4() throws DocumentException, IOException {
		  String pdfDownloadPath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\questionFolderDownload\\file.pdf";
		     
	        BaseFont baseFont = BaseFont.createFont("STSong-Light",
	        		"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	        Font font = new Font(baseFont, 10, Font.BOLD);
		  Document document = new Document(PageSize.A4);
		PdfWriter.getInstance(document, new FileOutputStream(pdfDownloadPath));
		document.open();
		Paragraph paragraph = new Paragraph();
		PdfPTable table = new PdfPTable(5);
		for (int aw = 0; aw < 10; aw++) {
			// 构建每一格
			table.addCell(new Phrase("试试",font));
		}
		for (int aw = 0; aw < 10; aw++) {
			// 构建每一格
			table.addCell(" ");
		}
		paragraph.add(table);
		document.add(paragraph);
		document.close();
	}
	
	@Test
	public void test5() throws DocumentException, IOException {
		  String pdfDownloadPath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\questionFolderDownload\\file.pdf";
		     
	        BaseFont baseFont = BaseFont.createFont("STSong-Light",
	        		"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	        Font font = new Font(baseFont, 10, Font.BOLD);
			Font    classtitleF = new Font(baseFont, 12, Font.BOLD);
		  Document document = new Document(PageSize.A4);
		PdfWriter.getInstance(document, new FileOutputStream(pdfDownloadPath));
		document.open();
        Paragraph p1 = new Paragraph(20);
        	Phrase phrase = new Phrase();
	        	phrase.add(new Chunk("2017-2018学年第一学期期末考试试卷",classtitleF));
	        	phrase.add(Chunk.NEWLINE);
	        	phrase.add( new Chunk("《操作系统》（A）卷",classtitleF));
	        	phrase.add(Chunk.NEWLINE);
	        	phrase.add(new Chunk("("+"（适用专业班级：网工15级①②班）"+")",classtitleF));
	        	phrase.add(Chunk.NEWLINE);
        p1.setAlignment(Element.ALIGN_CENTER);
        p1.add(phrase);
        
    	document.add(p1);
    	int k=5;
		Paragraph paragraph = new Paragraph();
	    PdfPTable table = new PdfPTable(k+3);
	    paragraph.setSpacingBefore(3);
	    paragraph.setSpacingAfter(3);
		table.addCell(new Phrase("题号",classtitleF));
		String[] s= {"一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四"};
		for(int i=0;i<k;i++) {
			table.addCell(new Phrase(s[i],classtitleF)); 
		}
		table.addCell(new Phrase("总分",classtitleF));
		table.addCell(new Phrase("复核人",classtitleF));
		
		table.addCell(new Phrase("得分",classtitleF));
		for(int i=0;i<=k+1;i++) {
			table.addCell(" ");
		}
		table.addCell(new Phrase("阅卷人",classtitleF));
		for(int i=0;i<=k+1;i++) {
			table.addCell(" ");
		}
		paragraph.add(table);
		document.add(paragraph);
		document.close();
	}
//	  @Test
//	  public static void main(String[] args) {
//		  String extension = "."+FilenameUtils.getExtension("qqq.txt");
//		  String newName=new SimpleDateFormat("yyyyMMddHH")+UUID.randomUUID().toString().replace("-","")+extension;
//		  String dateFormat=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
//		  String realpath="C:\\Users\\Administrator\\Desktop\\TestDB-SpringBoot\\TestDB_parent\\file\\"+dateFormat;
//		
//		  File file = new File(realpath,newName);
//		 if(!file.getParentFile().exists()) {
//			 file.getParentFile().mkdirs();
//		  }
//		  System.out.println("over");
//	  }
//	    public static void main(String[] args) throws DocumentException, IOException {
//	    	new test1().createPdf(RESULT);
//	    }
//	 
	    /**
	     *   创建PDF文档.
	     * @param filename 新PDF文档的路径
	     * @throws    DocumentException 
	     * @throws    IOException 
	     */
	    public void createPdf(String filename) throws DocumentException, IOException {
	    
	        Document document = new Document();
	        
	        // 第一步 创建文档实例
	        document.setPageSize(PageSize.A4);
	    
	        document.setMarginMirroring(true);

	        // 第二步 获取PdfWriter实例
	        PdfWriter.getInstance(document, new FileOutputStream(filename));
	        // 第三步 打开文档
	        document.open();
	        // 第四步 添加段落内容
	       
	        BaseFont baseFont = BaseFont.createFont("STSong-Light",
	        		"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	        Font font = new Font(baseFont, 10, Font.BOLD);
	 
			// 设置字体大小
			font.setSize(13);
			// 设置字体颜色
			font.setColor(new BaseColor(255, 0, 0));
			// 设置类型，为正常
			font.setStyle(Font.NORMAL);
			// 设置类型，加粗
			font.setStyle(Font.BOLD);
			// 设置类型，倾斜
			font.setStyle(Font.ITALIC);
			// 设置类型，下划线
			font.setStyle(Font.UNDERLINE);
			// 设置类型，可组合，倾斜+删除线
			font.setStyle(Font.ITALIC | Font.STRIKETHRU);
	        Chunk chunk = new Chunk("语句块");
			// 设置字体，字体定宽
			chunk.setFont(font);
			// 设置背景颜色
			chunk.setBackground(new BaseColor(0xFF, 0xFF, 0x00));
			Paragraph paragraph = new Paragraph();
			paragraph.add(chunk);
	        document.add(new Paragraph(chunk));
	        // 第五部 操作完成后必须执行文档关闭操作。
	        document.close();
	    }
	  

}
